// 预测红色连续不超过2次的group
const filterRedContinuousLimit2Group = (item) => {
  const [secondLast, last] = item.accuracy_array.slice(-2)
  return secondLast === 0 && last === 0
}

// 预测绿色回弹，红色不连续的group
const filterGreenReboundRedNonContinuousGroup = (item) => {
  const [secondLast, last] = item.accuracy_array.slice(-2)
  return secondLast === 0 && last === 1
}

// 预测绿色连续的group
const filterGreenContinuousGroup = (item) => {
  const [secondLast, last] = item.accuracy_array.slice(-2)
  return secondLast === 1 && last === 1
}

const sortResults = (a, b) => {
  if (b.accuracy_rate !== a.accuracy_rate) {
    return b.accuracy_rate - a.accuracy_rate // accuracy_rate 降序
  }
  return a.max_consecutive_incorrect - b.max_consecutive_incorrect // max_consecutive_incorrect 升序
}

// summaryArray 可能是 prev 或者 current 数据
// 第一次过滤数据
const filterForecaArray = (summaryArray, $sortResults) => {
  const _sortResults = $sortResults || sortResults
  // 预测红色连续不超过2次的group
  const redContinuousLimit2Group = summaryArray.filter(filterRedContinuousLimit2Group).sort(_sortResults)

  // 预测绿色回弹，红色不连续的group
  const greenReboundRedNonContinuousGroup = summaryArray
    .filter(filterGreenReboundRedNonContinuousGroup)
    .sort(_sortResults)

  // 预测绿色连续的group
  const greenContinuousGroup = summaryArray.filter(filterGreenContinuousGroup).sort(_sortResults)

  return {
    redContinuousLimit2Group,
    greenReboundRedNonContinuousGroup,
    greenContinuousGroup
  }
}

module.exports = { filterForecaArray }

// todo cyan: filter twentyfour group

// summaryArray 数据结构
// {
//   id: 301907,
//   history_result_id: 20,
//   time: '2022-01-20',
//   number: '19',
//   group_name: 'FOUR_RANGE_00000468_GROUPS',
//   group_type: 'four',
//   sub_index: 6,
//   accuracy: 0,
//   accuracy_array: [
//     1, 1, 1, 1, 0, 1, 0,
//     1, 1, 1, 1, 0, 1, 1,
//     1, 1, 1, 0, 1, 0
//   ],
//   correct_count: 15,
//   incorrect_count: 5,
//   accuracy_rate: 0.75,
//   max_consecutive_correct: 5,
//   max_consecutive_incorrect: 1,
//   consecutive_correct_segments: 5,
//   consecutive_incorrect_segments: 5,
//   avg_consecutive_correct: 3,
//   avg_consecutive_incorrect: 1,
//   current_consecutive_correct: 0,
//   current_consecutive_incorrect: 1
// }
